package com.siburian.MyStuff;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.graphics.BitmapFactory;
import java.util.ArrayList;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import java.util.Locale;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Toast;

public class BelajarMembaca extends Activity implements
TextToSpeech.OnInitListener{

	private TextToSpeech tts;
	int [] tempGambar;
	String [] arrTxt;
	byte[]  baca_drawable[];
	String temp = "";
	Button Previous;
	Button Next;
	Button PlaySound;
	ImageView learnPicture;
	TextView txtPetunjuk;
	public int tempPosition = 1;
	public int indexContent = 1;
	int tempIndex = 1;
	Bab_Pelajaran db_pelajaran;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.belajar_membaca);
        db_pelajaran = new Bab_Pelajaran(this);
        tts = new TextToSpeech(this, this);
        ArrayList<ArrayList<Object>> data = db_pelajaran.getBacaan(1, 1, 1);//

        byte[] image = Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image1 = Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image2= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image3= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image4= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image5= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image6= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image7= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image8= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image9= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        byte[] image10= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.data));
        
        
		if(data.size()==0){
			db_pelajaran.generateBab(image,image1,image2,image3,image4,image5,image6,image7,image8,image9,image10);
		}
		tempGambar = new int[data.size()];
		arrTxt = new String[data.size()];
		baca_drawable = new byte[1][data.size()];
		tempPosition = data.size();
		for (int posisi = 0; posisi < data.size(); posisi++) {
			ArrayList<Object> baris = data.get(posisi);			
			tempGambar[posisi] = Integer.parseInt(baris.get(0).toString());
			arrTxt[posisi] = baris.get(3).toString();
			//baca_drawable[posisi] = db_pelajaran.getImageFromDatabase(1);
			tempIndex++;
		}


		learnPicture = (ImageView) findViewById(R.id.belajar_img_membaca);
		txtPetunjuk =(TextView) findViewById(R.id.belajar_txt_membaca);
		txtPetunjuk.setText(arrTxt[indexContent]);
        Previous = (Button) findViewById(R.id.btnPrevious_belajar_membaca);
        Previous.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

//				if(indexContent == 1){
//					indexContent = tempPosition;
//					learnPicture.setImageBitmap(Utilities.getImage(db_pelajaran.getImageFromDatabase(indexContent)));
//					txtPetunjuk.setText(arrTxt[indexContent]);
//				}
					indexContent--;
					learnPicture.setImageBitmap(Utilities.getImage(db_pelajaran.getImageFromDatabase(tempGambar[indexContent])));
					txtPetunjuk.setText(arrTxt[indexContent]);
					

				
			}
		});
        Next = (Button) findViewById(R.id.btnNext_belajar_membaca);
        Next.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				if(indexContent == tempPosition){
//					indexContent = 1;
//					learnPicture.setImageBitmap(Utilities.getImage(db_pelajaran.getImageFromDatabase(indexContent)));
//					txtPetunjuk.setText(arrTxt[indexContent]);
//				}
//				else{
					indexContent++;
					learnPicture.setImageBitmap(Utilities.getImage(db_pelajaran.getImageFromDatabase(tempGambar[indexContent])));
					txtPetunjuk.setText(arrTxt[indexContent]);
					Toast.makeText(getBaseContext()," nilai dari indexContent " + indexContent,
							Toast.LENGTH_LONG).show();
//				}
			}
		});
        PlaySound = (Button) findViewById(R.id.btnSound_belajar_membaca);
        PlaySound.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getBaseContext()," Data yang ditampung " + txtPetunjuk.getText().toString(),Toast.LENGTH_LONG).show();
				speakOut();
			}
		});
    }
    
    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
 
    @Override
    public void onInit(int status) {
 
        if (status == TextToSpeech.SUCCESS) {
 
            int result = tts.setLanguage(Locale.US);
 
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
            	//PlaySound.setEnabled(true);
                speakOut();
            }
 
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
 
    }
 
    private void speakOut() {
 
        String text = txtPetunjuk.getText().toString();
        PlaySound.setEnabled(true);
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

}
